# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=pypng
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=0.20231004.0
pkgrel=1
pkgdesc='Pure Python library for PNG image encoding/decoding (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.com/drj11/pypng'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
options=('!strip')
source=("${url}/-/archive/${_realname}-${pkgver}/${_realname}-${_realname}-${pkgver}.tar.gz"
        "${url}/-/commit/e5b02fab72436f10b12e04ca6763b42495a586a3.patch")
sha256sums=('e32c8ea6a6801dadede06f9e0e9122b50d93405dd30c1e3c4f9f44bb94090a25'
            'b4d8b503cb42680ef9cd7ab7d43c18e5ff5c39b093c0895afb7bae8d3f074026')

prepare() {
  cd "${_realname}-${_realname}-${pkgver}"

  # In Sphinx doc, Use a configparser
  patch -p1 -i "${srcdir}"/e5b02fab72436f10b12e04ca6763b42495a586a3.patch
}

build() {
  cp -r "${_realname}-${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  python -m build --wheel --skip-dependency-check --no-isolation

  make -C man html
}

package_python-pypng() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENCE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}

package_python-pypng-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  cp -a "${srcdir}/python-build-${MSYSTEM}/man/_build/html" "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/html"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
